//
//  ImageVC.m
//  ox
//
//  Created by 喻杰 on 2016/11/18.
//  Copyright © 2016年 fengge. All rights reserved.
//

#import "ImageVC.h"


@interface ImageVC ()
    @property(nonatomic,strong) UIImageView * imageView;
@end

@implementation ImageVC

@synthesize image,imageView,url;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"那一刻";
    
    if(image){
        imageView = [[UIImageView alloc] initWithImage:image];
    }else{
        imageView = [[UIImageView alloc]init];
        
        [imageView sd_setImageWithURL:url];
    }
    imageView.userInteractionEnabled = YES;
    
    UIPinchGestureRecognizer * pinch =[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
    
    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    
    [imageView addGestureRecognizer:pinch];
    
    [imageView addGestureRecognizer:pan];
    
    [self.view addSubview:imageView];
    
  
    CGFloat height = imageView.image.size.height * self.view.bounds.size.width/imageView.image.size.width;
    
     CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
    
    CGFloat top =rectStatus.size.height + self.navigationController.navigationBar.frame.size.height;
    
    imageView.frame =CGRectMake(0,  top , self.view.bounds.size.width, height);
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void) pinch:(UIPinchGestureRecognizer*)pinch {
    
    DDLogInfo(@"scale=%f",pinch.scale);
    DDLogInfo(@"width = %f",imageView.frame.size.width);
    DDLogInfo(@"velocity = %f",pinch.velocity);

    if(pinch.velocity < 0 && imageView.frame.size.width < self.view.bounds.size.width/2 ){
        return ;
    }else if (pinch.velocity > 0 && imageView.frame.size.width > 1.5 * self.view.bounds.size.width){
        return;
    }
    
    imageView.transform =CGAffineTransformScale(imageView.transform, pinch.scale, pinch.scale);
}

-(void) pan:(UIPanGestureRecognizer*)pan {
    
    CGPoint pt = [pan translationInView:imageView];
    pan.view.center = CGPointMake(pan.view.center.x +pt.x , pan.view.center.y + pt.y);
    //每次移动完，将移动量置为0，否则下次移动会加上这次移动量
    [pan setTranslation:CGPointMake(0, 0) inView:self.view];
    if (pan.state == UIGestureRecognizerStateEnded) {
        DDLogInfo(@"pan.view == %f", pan.view.center.x);
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
@end
